package priv.cwr.springboot.demo.controller;

import com.github.pagehelper.Page;
import com.google.common.base.Preconditions;
import org.joda.time.DateTime;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import priv.cwr.springboot.demo.component.Errors;
import priv.cwr.springboot.demo.configuration.Global;
import priv.cwr.springboot.demo.exception.ApiException;
import priv.cwr.springboot.demo.model.db.UserEntity;
import priv.cwr.springboot.demo.model.vo.UserAddForm;
import priv.cwr.springboot.demo.model.vo.UserForm;
import priv.cwr.springboot.demo.model.vo.UserMoveForm;
import priv.cwr.springboot.demo.model.vo.base.PageNumber;
import priv.cwr.springboot.demo.model.vo.base.Result;
import priv.cwr.springboot.demo.service.UserService;

import javax.validation.Valid;

/**
 * User: R
 * Date: 2018/8/29
 * Time: 11:01
 * Created with IntelliJ IDEA.
 */
@RestController
@RequestMapping("/user")
@Validated
public class UserController {
    private final UserService userService;
    private final Errors errors;

    public UserController(UserService userService,
                          Errors errors) {
        this.userService = userService;
        this.errors = errors;
    }

    @PostMapping("/add")
    public ResponseEntity<Result<Long>> addUser(@Valid @RequestBody UserAddForm userAddForm,
                                                BindingResult bindingResult) {
        UserEntity userEntity = new UserEntity();
        userEntity.setName(userAddForm.getName());
        userEntity.setDeptId(userAddForm.getDeptId());
        userEntity.setPhone(userAddForm.getPhone());
        userEntity.setIntroduce(userAddForm.getIntroduce());
        userEntity.setCreateTime(DateTime.now().toDate());
        long id = this.userService.add(userEntity);
        return new ResponseEntity<>(new Result<>(id), HttpStatus.OK);
    }

    /**
     * 查询用户列表（PC端）
     *
     * @param name     用户姓名
     * @param phone    电话
     * @param deptId   部门id
     * @param page     分页参数
     * @param pagesize 分页参数
     * @return
     */
    @GetMapping("/find-all")
    public ResponseEntity<Result<Page<UserForm>>> findAll(@RequestParam(value = "name", required = false) String name,
                                                          @RequestParam(value = "phone", required = false) String phone,
                                                          @RequestParam(value = "dept_id", required = false) Long deptId,
                                                          @RequestParam(value = "page", required = false) Integer page,
                                                          @RequestParam(value = "pagesize", required = false) Integer pagesize) {
        if (page == null) {
            page = 1;
        }
        if (pagesize == null) {
            pagesize = 10;
        }
        Page<UserForm> list = this.userService.findAll(new PageNumber(page, pagesize), name, phone, deptId);
        return new ResponseEntity<>(new Result<>(list), HttpStatus.OK);
    }

    /**
     * 查询用户列表（移动端）
     *
     * @param deptId   部门id
     * @param page     分页参数
     * @param pagesize 分页参数
     * @return
     */
    @GetMapping("/find-all-app")
    public ResponseEntity<Result<Page<UserForm>>> findAllByApp(@RequestParam(value = "dept_id", required = false) Long deptId,
                                                               @RequestParam(value = "page", required = false) Integer page,
                                                               @RequestParam(value = "pagesize", required = false) Integer pagesize) {
        if (page == null) {
            page = 1;
        }
        if (pagesize == null) {
            pagesize = 10;
        }
        Page<UserForm> list = this.userService.findAllByApp(new PageNumber(page, pagesize), deptId);
        return new ResponseEntity<>(new Result<>(list), HttpStatus.OK);
    }


    /**
     * 查询单个用户
     *
     * @param id 用户id
     * @return
     */
    @GetMapping("/find-id/{id}")
    public ResponseEntity<Result<UserEntity>> findOne(@PathVariable Long id) {
        UserEntity userEntity;
        try {
            userEntity = Preconditions.checkNotNull(this.userService.findOne(id));
        } catch (NullPointerException e) {
            throw new ApiException("没有找到该用户", new Result(errors, Global.Errors.NOT_FOUND, HttpStatus.NOT_FOUND.value(), "用户"));
        }
        return new ResponseEntity<>(new Result<>(userEntity), HttpStatus.OK);
    }


    @PostMapping("/move")
    public ResponseEntity<Result<Boolean>> moveToDept(@RequestBody UserMoveForm userMoveForm) {
        boolean success = this.userService.moveToDept(userMoveForm);
        return new ResponseEntity<>(new Result<>(success), HttpStatus.OK);
    }
}
